<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="${CONTEXT_PATH}/" />
<title>班级信息管理</title>
	<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/icon.css">
	<script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
    
    <script type="text/javascript">
    	function searchGrade() {
    		$("#dg").datagrid('load',{
    			gradeName:$("#s_gradeName").val()
    		});
    	}
    	function removeGrade() {
    		var selectedRows = $('#dg').datagrid('getSelections');
    		if(selectedRows.length == 0) {
    			$.messager.alert("系统提示", "请选择要删除的数据！");
    			return;
    		}
    		var strIds = [];
    		for(var i=0; i<selectedRows.length; i++) {
    			strIds.push(selectedRows[i].id);
    		}
    		var ids = strIds.join(",");
    		$.messager.confirm("系统提示", "您确定要删除这<font color=red>"+selectedRows.length+"</font>条数据吗？", function(r) {
    			if(r) {
    				$.post("grade/remove", {delIds:ids}, function(result) {
    					if(result.success) {
    						$.messager.alert("系统提示", "您已成功删除<font color=red>"+result.delNums+"</font>条数据！");
    						$('#dg').datagrid("reload");
    					} else {
    						$.messager.alert("系统提示", '<font color=red>' + selectedRows[result.errorIndex].gradeName + '</font>'+result.errorMsg);
    					}
    				}, "json");
    			}
    		});	
    	}
    	function openGradeCreateDialog() {
    		$('#dlg').dialog('open').dialog('setTitle', '添加班级信息');
    		$('#fm').form('clear');
    		url = "grade/createOrUpdate";
    	}
    	function openGradeUpdateDialog() {
    		var selectedRows = $('#dg').datagrid('getSelections');
    		if(selectedRows.length != 1) {
    			$.messager.alert('系统提示', '请选择一条要编辑的数据！');
    			return;
    		}
    		var row = selectedRows[0];
    		
    		//取值方式一 row填充进表格 与jfinal getModel冲突 需要修改jfinal
    		/* $('#dlg').dialog('open').dialog('setTitle', '修改班级信息');
    		$('#fm').form('load', row); 
    		 */
    		//取值方式二 从数据库查询 使用el取值
    		
    		$('#dlg').dialog('open').dialog('setTitle', '修改班级信息').dialog('refresh','grade/readById?id='+row.id);
    		
    		url = "grade/createOrUpdate?grade.id="+row.id;
    	}
    	function createGrade() {
    		$('#fm').form('submit',{
    			url:url,
    			onSubmit:function() {
    				return $(this).form('validate');
    			},
    			success:function(result) {
    				var result = eval('(' + result + ')');// change the JSON string to javascript object
    				if(result.success) {
    					$.messager.alert("系统提示", "保存成功！");
    					closeGradeDialog();
    					$('#dg').datagrid('reload');
    				} else {
    					$.messager.alert("系统提示", result.errorMsg);
    				}
    			}
    		});
    	}
    	function closeGradeDialog() {
    		$("#dlg").dialog("close");
    	}
    	
    </script>
</head>
<body style="margin: 5px;">
	
	<table id="dg" title="班级信息" class="easyui-datagrid" fitColumns="true"
		pagination="true" rownumbers="true" fit="true" url="grade/read" toolbar="#tb">
		<thead>
			<tr>
				<th field="cb" checkbox="true"></th>
				<th field="id" width="50px;">编号</th>
				<th field="gradeName" width="100">班级名称</th>
				<th field="gradeDesc" width="250">班级描述</th>
			</tr>
		</thead>
	</table>
	<div id="tb">
		<div>
			<a href="javascript:openGradeCreateDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
			<a href="javascript:openGradeUpdateDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
			<a href="javascript:removeGrade()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
		</div>
		<div>&nbsp;班级名称：&nbsp;<input type="text" name="s_gradeName" id="s_gradeName"/><a href="javascript:searchGrade()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a> </div>
	</div>
	<div id="dlg" class="easyui-dialog" style="width: 400px;height: 280px;padding: 10px 20px"
		closed="true" buttons="#dlg-buttons">
		<form id="fm" method="post" >
			<table>
				<tr>
					<td>班级名称：</td>
					<td><input type="text" name="grade.gradeName" id="gradeName" class="easyui-validatebox" required="true"/></td>
				</tr>
				<tr>
					<td valign="top">班级描述：</td>
					<td><textarea rows="7" cols="30" name="grade.gradeDesc" id="gradeDesc"></textarea> </td>
				</tr>
			</table>
		</form>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:createGrade()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
		<a href="javascript:closeGradeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">取消</a>
	</div>
</body>
</html>